// pages/crash/crash.js
let handleNavigationBar = require("../../utils/formatCurrency").handleNavigationBar;
let download = require("../../utils/formatCurrency").download;
const app = getApp();
Page({
  data: {
    chartOrform:true,
    fixedText: '列表',
    cashList: [],
    chartData:{
      cash:'',
      qichu:'',
      qimo:''
    },
    cashFlowData: [
      { income: 0, out: '', diff: '', width: 0, title:'经营活动产生的现金流量净额'},
      { income: 0, out: '', diff: '', width: 0, title: '投资活动产生的现金流量净额'},
      { income: 0, out: '', diff: '', width: 0, title: '筹资活动产生的现金流量净额' }
    ],
    showButton: true,
    date: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    handleNavigationBar(app.globalData.company)

    this.data.date = options.data;
    wx.showLoading({
      title: '加载中',
    })
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/cash/find',
      method: 'POST',
      header: {
        token: app.globalData.token,
        version: app.globalData.version
      },
      data: {
        accountBookId: app.globalData.accountBookId,
        endPeriod: options.data
      },
      success: function (res) {
        that.handleData(res.data.cashList)
      }
    })
  },

  handleData(data){
    wx.hideLoading()
    this.data.chartData.cash = data[22].monthCashBD;
    this.data.chartData.qichu = data[23].monthCashBD;
    this.data.chartData.qimo = data[24].monthCashBD

    this.setData({
      cashList: data,
      chartData: this.data.chartData
    })

    if (data[22].monthCashBD < 0 || data[23].monthCashBD < 0 || data[24].monthCashBD<0) {
      this.setData({
        showButton: false,
        chartOrform: false
      })
      return
    }

    this.data.cashFlowData[0].income = data[1].monthCashBD + data[2].monthCashBD
    this.data.cashFlowData[0].out = data[3].monthCashBD + data[4].monthCashBD + data[5].monthCashBD + data[6].monthCashBD
    this.data.cashFlowData[0].diff = data[7].monthCashBD
    this.data.cashFlowData[0].width = (this.data.cashFlowData[0].out / this.data.cashFlowData[0].income*100).toFixed(2)

    this.data.cashFlowData[1].income = data[9].monthCashBD + data[10].monthCashBD + data[11].monthCashBD
    this.data.cashFlowData[1].out = data[12].monthCashBD + data[13].monthCashBD
    this.data.cashFlowData[1].diff = data[14].monthCashBD
    this.data.cashFlowData[1].width = (this.data.cashFlowData[1].out / this.data.cashFlowData[1].income * 100).toFixed(2)

    this.data.cashFlowData[2].income = data[16].monthCashBD + data[17].monthCashBD
    this.data.cashFlowData[2].out = data[18].monthCashBD + data[19].monthCashBD + data[20].monthCashBD
    this.data.cashFlowData[2].diff = data[21].monthCashBD
    this.data.cashFlowData[2].width = (this.data.cashFlowData[2].out / this.data.cashFlowData[2].income * 100).toFixed(2)

    this.setData({
      cashFlowData: this.data.cashFlowData
    })
  },

  swich() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    if (this.data.chartOrform) {
      this.setData({
        chartOrform: false,
        fixedText: '图表'
      })
    } else {
      this.setData({
        chartOrform: true,
        fixedText: '列表'
      })
    }
  }, 

  downLoad() {
    let url = app.globalData.url + 'api/export/pdf/cashflow?accountBookId=' + app.globalData.accountBookId + '&startPeriod=' + this.data.date + '&endPeriod=' + this.data.date + '&version=' + app.globalData.version + '&token=' + app.globalData.token
    download(url)
  }
})